package app.fugema.shop.main.home.view;

import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.FragmentHomeStoreBinding;
import app.fugema.shop.main.home.adapter.HomeStoreAdapter;
import app.fugema.shop.main.home.model.LocaCityBean;
import app.fugema.shop.main.home.model.NearStoreResult;
import app.fugema.shop.main.online.vm.OnlineVM;

import com.base.common.app.LoginUtils;
import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DensityUtil;
import com.base.common.utils.LogUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyLinearLayoutManager;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.base.BaseFragment;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;

import app.fugema.shop.R;

public class HomeStoreFragment extends BaseFragment<FragmentHomeStoreBinding, OnlineVM> implements OnGetDataListener
{
    private String userKey = "";
    @Override
    protected FragmentHomeStoreBinding initDataBinding(LayoutInflater inflater, ViewGroup container)
    {
        return DataBindingUtil.inflate(inflater, R.layout.fragment_home_store, container, false);
    }

    private HomeStoreAdapter storeAdapter;

    private int recyclerViewDy = 0;//上下滑动的距离

    private TencentLocationManager tlLocationMgr;

    private LocaCityBean cityBean;

    @Override
    public void initView()
    {
        super.initView();
        //设置状态栏高度
        int statusBarH = DensityUtil.getStatusBarHeight(getActivity());
        ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams) binding.statusBar.getLayoutParams();
        layoutParams.height = layoutParams.height + statusBarH;
        binding.statusBar.setLayoutParams( layoutParams );
        binding.rvRecyclerView.setLayoutManager( new MyLinearLayoutManager(getActivity()));
        storeAdapter = new HomeStoreAdapter();
        binding.rvRecyclerView.setAdapter(storeAdapter);
        viewUtils.setRefreshLayout(binding.rvRecyclerView, this);
        //发布问诊
        binding.btDiagnose.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {
                UIUtils.showToastSafes("发布问");
            }
        });


        binding.rvRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                /*
                recyclerViewDy += dy;
                if (recyclerViewDy > 200)
                {
                    binding.rtvDiagnose.setVisibility(View.GONE);
                    ViewUtils.setViewMarginStart(binding.ivDoctorIcon, DensityUtil.getDimens(R.dimen.dp_12), DensityUtil.getDimens(R.dimen.dp_12));
                }
                else
                {
                    binding.rtvDiagnose.setVisibility(View.VISIBLE);
                    ViewUtils.setViewMarginStart(binding.ivDoctorIcon, DensityUtil.getDimens(R.dimen.dp_25), DensityUtil.getDimens(R.dimen.dp_0));
                }
                */
            }
        });

        tlLocationMgr = TencentLocationManager.getInstance(getActivity());
        cityBean = new LocaCityBean();
        // 选择完 地区之后
        LiveEventBus.get(AppConstants.EventKey.CITY_SELECT , LocaCityBean.class)
                    .observe(this,cityResult ->{

                        cityBean = cityResult;
                        cityBean.setItemType(BaseItemMultiType.TYPE_HEAD_ONE);
                        storeAdapter.addHeaderView(cityBean,0);
                        // 拿到经纬度 在请求数据
                        if( cityBean.getLatitude() != 0 && cityBean.getLongitude() != 0 )
                        {
                            onGetData(1);
                        }
                    });
    }

    @Override
    public void initData()
    {
        super.initData();

        if( LoginUtils.isLogin() )
        {
            userKey = LoginUtils.getToken();
        }

        // 首先打开 单次定位
        tlLocationMgr.requestSingleFreshLocation(null, new TencentLocationListener() {
            @Override
            public void onLocationChanged(TencentLocation tencentLocation, int i, String s)
            {
                //LogUtil.e("tlLocationMgr","city = " + tencentLocation.getCity() + " District = " + tencentLocation.getDistrict() );
                //LogUtil.e( "s = " + JacksonUtils.transBean2Json( tencentLocation ));
                cityBean.setCityName(tencentLocation.getCity());
                cityBean.setLatitude( tencentLocation.getLatitude() );
                cityBean.setLongitude( tencentLocation.getLongitude() );
                cityBean.setItemType(BaseItemMultiType.TYPE_HEAD_ONE);
                storeAdapter.addHeaderView(cityBean,0);
                // 拿到经纬度 在请求数据
                if( cityBean.getLatitude() != 0 && cityBean.getLongitude() != 0 )
                {
                    onGetData(1);
                }
            }

            @Override
            public void onStatusUpdate(String s, int i, String s1)
            {}
        }, Looper.getMainLooper());

    }

    @Override
    public void onGetData(int pn)
    {
        // 昆明 才有数据   24.87966,102.83322
        viewModel.queryNearStoreList(pn,cityBean.getLatitude(),cityBean.getLongitude(),userKey )
                .observe(this, new BaseViewObserver<LiveDataWrapper<NearStoreResult>>(viewUtils.getStateLayout())
                {
                    @Override
                    protected void onSuccess(LiveDataWrapper<NearStoreResult> data)
                    {
                        /*
                        List<StoreBean> list = new ArrayList<>();
                        for (StoreBean store : data.data.getStores())
                        {
                            //store.getShopBranch()
                            list.add( store);
                        }

                        if (onlineAdapter.getChildCount() == 0)
                        {
                            onlineAdapter.setDataList(list);
                        }
                        else
                        {
                            onlineAdapter.addList(1, list);
                        }
                        */
                        if( data.data.isSuccess() )
                        {
                            viewUtils.setDataListRefreshLayout(storeAdapter,pn,5,data.data.getStores()  );
                        }
                    }

                    @Override
                    public boolean isRefreshLayoutData()
                    {
                        return true;
                    }

                    @Override
                    public boolean isEmptyData(LiveDataWrapper<NearStoreResult> data)
                    {
                        return pn==1 && UIUtils.isEmpty(data.data.getStores());
                    }
                });
    }
}
